<template>
  <div>
    <div v-if="keyList.length" >
      <template v-for="item in handlData(keyList)" >
        <FieldCollectionData
          v-if="!item.platform || item.platform.length === 0 || (item.platform && item.platform.includes(Number(platform)))"
          :key="item.key"
          :item="item"
          :column="column"
          :scope="scope" />
      </template>
      <el-popover
        v-if="keyList.length > 4"
        placement="bottom"
        width="240"
        trigger="click">
        <div v-for="item in keyList" :key="item.key">
          <FieldCollectionData
            v-if="!item.platform || item.platform.length === 0 || (item.platform && item.platform.includes(Number(platform)))"
            :item="item"
            :column="column"
            :scope="scope"
            style="margin-top: 5px" />
        </div>
        <div slot="reference" class="more-btn">更多 <i class="el-icon-arrow-right"/></div>
      </el-popover>
    </div>
    <div v-else>--</div>
  </div>
</template>
<script>
import FieldCollectionData from './fieldCollectionData';
import { moneyFilter } from '@/filters/number';
import { journalStatusFilter, selectStatusFilter, autoRenewalFilter } from '@/utils/const/resouce';
import { cooperationStatusFilter } from '@/utils/accountCommonData';

export default {
  components: { FieldCollectionData },
  filter: {
    journalStatusFilter, selectStatusFilter, cooperationStatusFilter
  },
  props: {
    column: {
      type: Object,
      default: () => { return {}; }
    },
    scope: {
      type: Object,
      default: () => { return {}; }
    },
    // 平台，1:微信 2: 微博 3:小红书 4.抖音 5:更多  6:直播 7:B站
    platform: {
      type: String | Number,
      default: ''
    }
  },
  data() {
    return {
    };
  },
  computed: {
    keyList() {
      const column = this.column;

      // platform 平台，1:微信 2: 微博 3:小红书 4.抖音 5:更多  6:直播 7:B站
      switch (column.fieldName) {
        case 'monthReportPrice':
        case 'nextMonthReportPrice':
        case 'monthNotReportPrice':
        case 'nextMonthNotReportPrice':
          if (Number(this.platform) !== 3) return [];
          return this.getPriceKey(column.fieldName);
        case 'articlesPrice':
          if (![4, 7, 2].includes(Number(this.platform))) return [];
          return this.getPriceKey(column.fieldName);
        case 'microTaskCost':
          if (![2].includes(Number(this.platform))) return [];
          return this.getPriceKey(column.fieldName, true);
        case 'monthReportPriceCost':
        case 'nextMonthReportPriceCost':
        case 'monthNotReportPriceCost':
        case 'nextMonthNotReportPriceCost':
          if (Number(this.platform) !== 3) return [];
          return this.getPriceKey(column.fieldName, true);
        case 'costPrice':
          if (![4, 7, 2].includes(Number(this.platform))) return [];
          return this.getPriceKey(column.fieldName, true);
        case 'relevantUser':
        // 相关人员
          return [
            { key: 'accountOperation', label: '其他运营人员', type: 'userArr', labelName: 'value' },
            { key: 'totalOperation', label: '所有人员', type: 'userArr', labelName: 'value' },
            { key: 'followUsers', label: '跟进人', type: 'userArr', labelName: 'name' }
          ];
        case 'updateInfo':
          // 更新信息
          return [
            { key: 'updator', label: '更新人', type: 'user', labelName: 'updator', keyName: 'updatorId' },
            { key: 'updateTime', label: '更新时间', type: 'time' }
          ];
        case 'systemAllInfo':
          return [
            { key: 'createName', label: '创建人', type: 'user', labelName: 'createName', keyName: 'createUserId' },
            { key: 'createTime', label: '创建时间', type: 'time' },
            { key: 'systemId', label: '系统ID' }
          ];
        case 'signDetailStatus':
          return [
            { key: 'signDetailStatus', label: '签约状态', type: 'sign' },
            { key: 'isExpired', label: '到期时长', type: 'isExpired' },
            { key: 'signGroupsName', label: '签约所属部门', parentField: 'additionalKey' },
            { key: 'signDate', start: 'signStartDate', end: 'signEndDate', label: '签约有效期', parentField: 'additionalKey', format: 'YYYY/MM/DD', type: 'rangeTime' },
            { key: 'cooperationTime', start: 'cooperationStart', end: 'cooperationEnd', label: '试合作期', parentField: 'additionalKey', format: 'YYYY/MM', type: 'rangeTime' },
            { key: 'cooperationOption', label: '合作方式', parentField: 'additionalKey', filter: cooperationStatusFilter },
            { key: 'workWxCode', label: '工作微信', parentField: 'additionalKey' }
          ];
        case 'contractBroker':
          return [
            { key: 'contractBroker', label: '签单签约经纪', parentField: 'additionalKey' },
            { key: 'followContractBroker', label: '现维护经纪人', parentField: 'additionalKey', type: 'userArr' },
            { key: 'executeBroker', label: '执行经纪', parentField: 'additionalKey', type: 'userArr' },
            { key: 'operator', label: '达人运营', parentField: 'additionalKey', type: 'userArr' }
          ];
        case 'otherAccountInfo':
          // 更多账号信息，也就是之前在基础信息中的 自定义字段
          return this.getDynamicKey('baseInfo');
        case 'otherOpeatorInfo':
          return [
            { key: 'priceTime', label: '报价有效期', type: 'priceTime' },
            { key: 'payMethod', label: '支付方式', parentField: 'invoiceInfo' },
            { key: 'taxPoint', label: '票面税点', parentField: 'invoiceInfo', unit: '%' },
            { key: 'actualTaxPoint', label: '实际税点', parentField: 'invoiceInfo', unit: '%' },
            { key: 'mainName', label: '主体名(即对方公司全称)', parentField: 'invoiceInfo' },
            { key: 'organization', label: '所属机构', parentField: 'sensitivityInfo' },
            { key: 'businessContact', label: '商务联系人', parentField: 'sensitivityInfo' }
          ];
        case 'otherPriceInfo':
          // 其他价格信息，也就是之前在价格信息中的 自定义字段
          return this.getDynamicKey('priceInfo');
        case 'priceInfo':
          return [
            { key: 'profitSharingRemark', label: '分成说明', parentField: 'priceInfo' },
            { key: 'profitSharingRatio', label: '分成比例', parentField: 'priceInfo', type: 'profitSharingRatio' },
            { key: 'guaranteeRemark', label: '保底说明', parentField: 'priceInfo' },
            { key: 'guaranteePrice', label: '保底金额', parentField: 'priceInfo', unit: '元' }
          ];
        case 'nowSignStatus':
          return [
            { key: 'autoRenewal', label: '自动续约', parentField: 'additionalKey', filter: autoRenewalFilter },
            { key: 'signLink', label: '关联签约', parentField: 'additionalKey', type: 'related', snKey: 'sn', routeName: 'signUpDetail', idKey: 'signUpId' },
            { key: 'terminationLink', label: '关联解约', parentField: 'additionalKey', type: 'related', snKey: 'sn', routeName: 'terminationDetail', idKey: 'terminationId' },
            { key: 'nowSignStatusLink' }
          ];
        case 'journalStatus':
          return [
            { key: 'selectionStatus', label: '选号状态', parentField: 'additionalKey', filter: selectStatusFilter },
            { key: 'journalStatus', label: '上刊状态', parentField: 'additionalKey', filter: journalStatusFilter },
            // { key: 'publicationTime', label: '上刊日期', parentField: 'additionalKey', type: 'time', format: 'YYYY/MM/DD' },
            { key: 'publicationMonth', label: '上刊月份', parentField: 'additionalKey', type: 'time', format: 'YYYY/MM' }
          ];
      }
      return [];
    }
  },
  methods: {
    handlData(list) {
      if (list.length <= 4) return list;
      return list.slice(0, 3);
    },
    getDynamicKey(key) {
      const data = this.scope.row[key];
      if (data && data.dynamicKey && data.dynamicKey.length) {
        return data.dynamicKey.map(i => {
          return {
            key: i.id,
            label: i.name,
            value: this.formatValue(i),
            type: 'dynamicKey'
          };
        });
      }
      return [];
    },
    getPriceKey(type, isCost = false) {
      // platform 平台，1:微信 2: 微博 3:小红书 4.抖音 5:更多  6:直播 7:B站
      let field = [];
      const rowData = this.scope.row;
      if (type === 'microTaskCost') {
        field = [
          { key: 'publish', label: '直发', parentField: 'additionalKey' },
          { key: 'forward', label: '转发', parentField: 'additionalKey' }
        ];
      } else if (Number(this.platform) === 3) {
        // 小红书
        field = [
          { key: 'imageTextPrice', label: '图文', parentField: 'additionalKey' },
          { key: 'videoPrice', label: '视频', parentField: 'additionalKey' }
        ];
      } else if (Number(this.platform) === 4) {
        // 抖音
        field = [
          { key: 'starMapTwenty', label: '星图报价1-20s', parentField: 'additionalKey' },
          { key: 'starMapSixty', label: '星图报价20-60s', parentField: 'additionalKey' },
          { key: 'starMapAboveSixty', label: '星图报价60s以上', parentField: 'additionalKey' },
          { key: 'contentCollectionPrice', label: '内容合集单坑位报价', parentField: 'additionalKey' }
        ];
      } else if (Number(this.platform) === 7) {
        // B站
        field = [
          { key: 'customVideo', label: '定制视频', parentField: 'additionalKey' },
          { key: 'implantationVideo', label: '植入视频', parentField: 'additionalKey' },
          { key: 'publishTrends', label: '直发动态', parentField: 'additionalKey' },
          { key: 'forwardTrends', label: '转发动态', parentField: 'additionalKey' }
        ];
      } else if (Number(this.platform) === 2) {
        // 微博
        field = [
          { key: 'publish', label: '非原创直发', parentField: 'additionalKey' },
          { key: 'forward', label: '非原创转发', parentField: 'additionalKey' },
          { key: 'originalContent', label: '原创图文发布', parentField: 'additionalKey' },
          { key: 'originalVideo', label: '原创视频发布', parentField: 'additionalKey' }
        ];
      }
      if (field.length) {
        return field.map(i => {
          i.secondParentField = type;
          if (isCost) {
            i.type = 'costField';
            if (rowData[i.parentField] && rowData[i.parentField][i.secondParentField] && rowData[i.parentField][i.secondParentField][i.key]) {
              i.value = rowData[i.parentField][i.secondParentField][i.key].value;
              i.costType = rowData[i.parentField][i.secondParentField][i.key].costType;
              if (i.costType === 1) {
                i.unit = '%';
              }
            } else {
              i.value = '';
            }
          }
          i.filter = (num) => {
            // let formatNum = Number(num);
            // console.log(num);
            // if (formatNum && !Number.isNaN(formatNum)) {
            //   const isNegative = formatNum < 0 ? '-' : '';
            //   formatNum = Math.abs(formatNum);
            //   if (formatNum >= 10000 && formatNum < 100000000) {
            //     formatNum = moneyFilter(formatNum / 10000) + '万';
            //   } else if (formatNum >= 100000000) {
            //     formatNum = moneyFilter(formatNum / 100000000) + '亿';
            //   }
            //   return isNegative + formatNum;
            // } else {
            //   return num;
            // }
            return moneyFilter(num, true);
          };
          return i;
        });
      }
      return field;
    },
    formatValue(data) {
      if (data.options && data.options.length > 0) {
        let res = '';
        data.options.forEach(item => {
          if (data.value === item.value) res = item.label;
        });
        return res;
      } else {
        return data.value;
      }
    }
  }
};
</script>
<style lang="scss" scoped>
@import './style.scss';
.more-btn {
  display: inline-flex;
  align-items: center;
  color: #406EFF;
  font-size: 12px;
  cursor: pointer;
}

</style>
